package com.lin.androidgame21;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Rect;


/***

 *Create By LWL Write

 **/

class TiledLayer {
    private Bitmap bitmap;
    private int width;
    private int height;
    private int x;
    private int y;
    private int[] tiledX;
    private int[] tiledY;
    private Rect src;
    private Rect dst;
    private int cols;
    private int rows;
    private int[][] tiledCell;

    TiledLayer(Bitmap bitmap, int width, int height, int cols, int rows) {
        this.height = height;
        this.width = width;
        this.bitmap = bitmap;
        this.cols = cols;
        this.rows = rows;
        int w = bitmap.getWidth() / width;
        int h = bitmap.getHeight() / height;
        tiledX = new int[w * h + 1];
        tiledY = new int[w * h + 1];
        for (int i = 0; i < h; i++) {
            for (int j = 0; j < w; j++) {
                tiledX[w * i + j + 1] = width * j;
                tiledY[w * i + j + 1] = height * i;
            }
        }
        src = new Rect();
        dst = new Rect();
        tiledCell = new int[rows][cols];
    }

    public int getTiledCellIndex(int row,int col){
        return tiledCell[row][col];
    }

    public Bitmap getBitmap() {
        return bitmap;
    }

    public void setBitmap(Bitmap bitmap) {
        this.bitmap = bitmap;
    }

    public int getX() {
        return x;
    }

    public void setX(int x) {
        this.x = x;
    }

    public int getY() {
        return y;
    }

    public void setY(int y) {
        this.y = y;
    }

    public int getWidth() {
        return width;
    }

    public void setWidth(int width) {
        this.width = width;
    }

    public int getHeight() {
        return height;
    }

    public void setHeight(int height) {
        this.height = height;
    }


    public int getCols() {
        return cols;
    }

    public void setCols(int cols) {
        this.cols = cols;
    }

    public int getRows() {
        return rows;
    }

    public void setRows(int rows) {
        this.rows = rows;
    }

    public int[][] getTiledCell() {
        return tiledCell;
    }

    public void setTiledCell(int[][] tiledCell) {
        this.tiledCell = tiledCell;
    }


    public void setPosition(int x, int y) {
        this.x = x;
        this.y = y;
    }


    public void outOfBounds() {
        if (getX() > 0) {
            setX(0);
        } else if (getX() < 1280 - cols * width) {
            setX(1280 - cols * width);
        }
        if (getY()>0){
            setY(0);
        }else if (getY()<768-rows*height){
            setY(768-rows*height);
        }

    }

    public void draw(Canvas canvas) {
        for (int i = 0; i < rows; i++) {
            for (int j = 0; j < cols; j++) {
                int tiledIndex = tiledCell[i][j];
                if (tiledIndex == 0)
                    continue;
                src.set(tiledX[tiledIndex], tiledY[tiledIndex],
                        tiledX[tiledIndex] + getWidth(),
                        tiledY[tiledIndex] + getHeight());
                dst.set(getX() + j * getWidth(), getY() + i * getHeight(),
                        getX() + (j + 1) * getWidth(),
                        getY() + (i + 1) * getHeight());
                canvas.drawBitmap(bitmap, src, dst, null);
            }
        }
    }

    public void move(float distanceX, float distanceY) {
        x += distanceX;
        y += distanceY;
        outOfBounds();
    }


}
